package org.xtframe.web.action;

import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.history.HistoryTask;
import org.jbpm.api.history.HistoryTaskQuery;
import org.springframework.stereotype.Repository;
import org.xtframe.util.ContextUtil;
import org.xtframe.util.JbpmUtil;

@Repository("/processImageAction")
public class ProcessImageAction {
	
	private String id;
	
	public String getImageByProcessDefinition() {
		try {
			if (ContextUtil.getCurrentUserData() == null) {
				return null;
			}
			HttpServletResponse response = ServletActionContext.getResponse();
			setHeader(response);
			
			RepositoryService repositoryService = JbpmUtil.getInstance().getRepositoryService();
			ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
					.processDefinitionId(id).uniqueResult();
			InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), 
					processDefinition.getImageResourceName());
			if(inputStream != null) {
				byte[] bytes = new byte[1024*4];
				int size = 0;
				//输出流程图
				while ((size = inputStream.read(bytes, 0, 1024*4)) != -1) {
					response.getOutputStream().write(bytes,0,size);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			id = null;
		}
		return null;
	}
	
	public String getImageByProcessInstance() {
		try {
			if (ContextUtil.getCurrentUserData() == null) {
				return null;
			}
			HttpServletResponse response = ServletActionContext.getResponse();
			setHeader(response);
			
			RepositoryService repositoryService = JbpmUtil.getInstance().getRepositoryService();
			String processDefinitionId = JbpmUtil.getInstance().findProcessDefinitionId(id);
			ProcessDefinition processDefinition = repositoryService
					.createProcessDefinitionQuery().processDefinitionId(
							processDefinitionId).uniqueResult();
			InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), 
					processDefinition.getImageResourceName());
			if(inputStream != null) {
				byte[] bytes = new byte[1024*4];
				int size = 0;
				//输出流程图
				while ((size = inputStream.read(bytes, 0, 1024*4)) != -1) {
					response.getOutputStream().write(bytes,0,size);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			id = null;
		}
		return null;
	}
	
	public String getImageByTask() {
		try {
			if (ContextUtil.getCurrentUserData() == null) {
				return null;
			}
			HttpServletResponse response = ServletActionContext.getResponse();
			setHeader(response);
			
			RepositoryService repositoryService = JbpmUtil.getInstance().getRepositoryService();
			HistoryTaskQuery historyTaskQuery = JbpmUtil.getInstance().getHistoryService().createHistoryTaskQuery();
			historyTaskQuery.taskId(id);
			HistoryTask historyTask = historyTaskQuery.uniqueResult();
			String processDefinitionId = JbpmUtil.getInstance().findProcessDefinitionId(historyTask.getExecutionId());
			ProcessDefinition processDefinition = repositoryService
					.createProcessDefinitionQuery().processDefinitionId(
							processDefinitionId).uniqueResult();
			InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), 
					processDefinition.getImageResourceName());
			if(inputStream != null) {
				byte[] bytes = new byte[1024*4];
				int size = 0;
				//输出流程图
				while ((size = inputStream.read(bytes, 0, 1024*4)) != -1) {
					response.getOutputStream().write(bytes,0,size);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			id = null;
		}
		return null;
	}
	
	public void setHeader(HttpServletResponse response) {
		// 阻止生成页面被缓存，保证每次重新生成随机验证码
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/png");
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}
	
}
